<!--
 * @Date: 2021-10-27 09:15:50
 * @LastEditors: 邓春宜
 * @Desc: 单曲线样式2，有坐标轴
 * @LastEditTime: 2021-11-05 23:08:11
 * @FilePath: \tianjushi-kanban\src\components\charts\SingleLine2.vue
-->
<template>
<div :style="[{ width: width, height: height,overflow:'hidden' }]">
  <div
    ref="echartref"
    :style="[{ width: width, height: height }, _resetChartStyle]"
  ></div>
</div>
  
</template>

<script>
import * as echarts from "echarts";

import { onMounted,toRefs, ref,reactive,watch,computed } from 'vue';
import { resetChartStyle, showLoading } from "@/core/tools/common";
export default {  
  
  props: {
      title: {
      type: String,
      default: "标题",
    },
      width: {
      type: String,
      default: "100%",
    },
     height: {
      type: String,
      default: "100%",
    },
    chartdata:{
     type: Array,
      default:   {'xData':["1月", "2月","3月","4月","5月","6月","7月","8月","9月","10月","11月","12月"],
	       'yData':[200, 300,400,200, 300,400,200, 300,400,200, 300,400],}
    },
    color: {
      type: Array,
      default: ['#1BC85E', '#00A0E9', '#fccb05', '#f5804d'],
    },
    min: {
      type: Number,
      default: 0,
    },
  },
  setup: function (props, context) {
	 let chartInit = null ;
    /*监听props*/
    watch(props, (newProps, oldProps) => {
        f_echartInit(chartInit)
    }); 
	
    let state = reactive({ 
      echartref: ref(), 
    }) 

    const _resetChartStyle = computed({
      get() {
        return resetChartStyle()
      } 
    })  
    const f_echartInit = (chartdom) =>{   
      let data_chart = props.chartdata;  
 
      let option = {
        tooltip: {
          trigger: "axis",
          axisPointer: {
            type: "shadow",
            textStyle: {
              color: "#fff",
            },
          },
        },
        grid: {
          borderWidth: 0,
          top: 90,
          bottom: 95,
          left: 80,
          textStyle: {
            color: "#fff",
          },
        },
        legend: {
          left: "center",
          top: "5px",
          textStyle: {
            color: "#90979c",
            fontSize: 18,
          },
          data: [props.title],
        },

        calculable: true,
        xAxis: [
          {
            type: "category",
            axisLine: {
              lineStyle: {
                color: "#fff",
              },
            },
            axisLabel: {
              show: true,
              textStyle: {
                fontSize: 18, //更改坐标轴文字大小
              },
            },
            splitLine: {
              show: false,
            },
            axisTick: {
              show: false,
            },
            data: data_chart.xData,
          },
        ],

        yAxis: [
          {
            type: "value",
            show: true,
            splitLine: {
              show: false,
            },
            min: props.min,
            scale: true,
            axisLine: {
              show: true,
              lineStyle: {
                color: "#fff",
                width: 1,
              },
            },
            axisLabel: {
              textStyle: {
                fontSize: 18, //更改坐标轴文字大小
              },
              formatter: "{value}%",
            },
          },
        ],
        series: [
          {
            name: props.title,
            type: "line",
            symbolSize: 10,
            symbol: "circle",
            itemStyle: {
              color: "#009DFF",
            },
            markPoint: {
              label: {
                normal: {
                  textStyle: {
                    color: "#fff",
                    fontSize: 16,
                  },
                },
              },
              symbolSize: 70,
              data: [
                {
                  type: "max",
                  name: "最大值",
                },
                {
                  type: "min",
                  name: "最小值",
                },
              ],
            },
            data: data_chart.yData,
          },
        ],
      };
      chartdom.setOption(option);
      chartdom.hideLoading(); 
    }
    //挂载
    onMounted(()=>{ 
      chartInit = echarts.init(state.echartref); 
      chartInit.showLoading(showLoading());
      f_echartInit(chartInit)
    }) 
    return {
      ...toRefs(state), 
      _resetChartStyle
    };  

  }, 
 
};
</script>

<style>
</style>